#include "main.h"
#include "gpio.h"
#include "stdlib.h"
#include <hic.h>

/*
#define ValBit(VAR,Place)         ( VAR & (1 < <Place) )
#define MskBit(Dest,Msk,Src)      ( Dest = (Msk & Src)  | ((~Msk) & Dest) ) 
#define AffBit(VAR,Place,Value)   ((Value)?(VAR|=(1<<Place)):(VAR&=((1<<Place)^255))) 

*/
UINT08 Plate_address1=0x00;
UINT16 tim3_tim=0;
UINT08 lock_number=0;
UINT08 time_ov3=0x00;

/*********************************************************************************
*   �� �� ��: delay_us
*   ����˵��: ΢����ʱ����ע��˺��������л���Ϊ(16Mʱ���ٶ�)
*   ��    �Σ�nCountҪ��ʱ��΢����������nCount=1΢��
*   �� �� ֵ: ��
*********************************************************************************/
void delay_us(UINT16 kk)   
{
   UINT16 j1=0,i1=0;
   for(i1=0;i1<kk;i1++)
   {
	   for(j1=0;j1<540;j1++)
		   {
			  ;
		   }
   }
   Clear_Wdt();
}


void Read_Address_IO(void)
{
 PET0=1;
 PET1=1;
 PET2=1;
 PET3=1;
}

void Channe_driver(void)
{
	
	PJT0=0;       
	PJ0=1;
	
	PFT0=0;
	PF0=1;
	PFT2=0;
	PF2=1;
	PFT4=0;
	PF4=1;

	PJT3=0;
	PJ3=1;
	PET5=0;
	PE5=1;
	PGT5=0;
	PG5=0;
	PDT1=0;
	PD1=1;
	
	PDT3=0;
	PD3=1;
	PDT5=0;
	PD5=1;
	PDT7=0;
	PD7=1;
	PJT5=0;
	PJ5=1;

	PJT7=0;
	PJ7=1;
	PCT1=0;
	PC1=1;
	PCT3=0;
	PC3=1;

	PCT5=0;
	PC5=1;
	
	PCT7=0;
	PC7=1;
	PBT1=0;
	PB1=1;
	PHT0=0;
	PH0=1;
	PHT2=0;
	PH2=1;

	PBT3=0;
	PB3=1;
	PBT5=0;
	PB5=1;
	PBT7=0;
	PB7=1;
	PAT1=0;
	PA1=1;
	
	PAT3=0;
	PA3=1;
	PAT5=0;
	PA5=1;
	PAT7=0;
	PA7=1;
	PHT5=0;
	PH5=1;

	PHT7=0;
	PH7=1;
	PGT1=0;
	PG1=1;
}

void Input_channe(void)
{
	
	PJT1=1;
	PFT1=1;
	PFT3=1;
	PJT2=1;
	
	PET4=1;
	PGT6=1;
	PDT0=1;
	PDT2=1;
	
	PDT4=1;
	PDT6=1;
	PJT4=1;
	PJT6=1;
	
	PCT0=1;
	PCT2=1;
	PCT4=1;
	PCT6=1;
	
	PBT0=1;
	PBT2=1;
	PHT1=1;
	PHT3=1;
	
	PBT4=1;
	PBT6=1;
	PAT0=1;
	PAT2=1;
	
	PAT4=1;
	PAT6=1;
	PHT4=1;
	PHT6=1;
	
	PGT0=1;
	PGT4=1;
}

void Out_RS485EN(void)
{

	PFT5=0;
	PF5 = 0;
	
	
	
	
    
}

sbit   bit_status0;
void Read_addr_code(void)
{
        UINT08 Plate_address2=0x00;
		__Asm  CWDT;

		bit_status0=0;
		bit_status0=PE3;
		if(bit_status0 != 0)
		SetBit(Plate_address2,0);
		else
		ClrBit(Plate_address2,0);

		bit_status0=0;
		bit_status0=PE2;
		if(bit_status0 != 0)
		SetBit(Plate_address2,1);
		else
		ClrBit(Plate_address2,1);

		bit_status0=0;
		
		bit_status0=PE1;
		if(bit_status0 != 0)
		SetBit(Plate_address2,2);
		else
		ClrBit(Plate_address2,2);
		__Asm  CWDT;

		bit_status0=0;
		
		bit_status0=PE0;
		if(bit_status0 != 0)
		SetBit(Plate_address2,3);
		else
		ClrBit(Plate_address2,3);

		Plate_address1=Plate_address2;
}

sbit   bit_status1;
sbit   bit_old1;

void Read_channes1_8state(UINT08 data0,UINT08 data1)
{
       if(data0==1)
       {
			if(data1==0)
			{
                PJ0 = 0;
			}
			else if(data1==1)
			{
				PJ0 = 1;
			}				
        }
       else if(data0==2)
       {
				if(data1==0)
				{
                PF0 = 0;
				}
				else  if(data1==1)
				{
				  PF0 = 1;
				}				
        }
	    else if(data0==3)
        {
				if(data1==0)
				{
                PF2 = 0;
				}
				else if(data1==1)
				{
				PF2 = 1;
				}
				
        }
		else if(data0==4)
        {
				if(data1==0)
				{
                  PF4 = 0;
				}
				else if(data1==1)
				{
				  PF4 = 1;
				}
        }
        if(data0==5)
       {
				if(data1==0)
				{
                PJ3 = 0;	
				}
				else if(data1==1)
				{
				PJ3 = 1;	
				}
        }
       else if(data0==6)
       {
				if(data1==0)
				{
                 PE5 = 0;
				}
				else if(data1==1)
				{
				 PE5 = 1;
				}
        }
	    else if(data0==7)
        {
				if(data1==0)
				{
                PG5 = 0;
				}
				else if(data1==1)
				{
				PG5 = 1;
				}
        }
		else if(data0==8)
        {
				if(data1==0)
				{
                PD1 = 0;
				}
				else if(data1==1)
				{
				PD1 = 1;
				}				
        }
}

sbit   bit_status2;
sbit   bit_old2;

void Read_channes9_16state(UINT08 data2,UINT08 bai1)
{
	   if(data2==0x01)
       {
				if(bai1==0)
				{
                PD3 = 0;
				}
				else if(bai1==1)
				{
				PD3 = 1;
				}
        }
	   if(data2==0x02)
       {	
				if(bai1==0)
				{
                PD5 = 0;
				}
				else if(bai1==1)
				{
				PD5 = 1;
				}
        }
      if(data2==0x03)
       {
				if(bai1==0)
				{
                PD7 = 0;
				}
				else if(bai1==1)
				{
				PD7 = 1;
				}				
        }
      if(data2==0x04)
       {
				if(bai1==0)
				{
                PJ5 = 0;
				}
				else if(bai1==1)
				{
				PJ5 = 1;
				}
        }
       if(data2==0x05)
       {
				if(bai1==0)
				{
                PJ7 = 0;
				}
				else if(bai1==1)
				{
				PJ7 = 1;
				}				
        }	
	if(data2==0x06)
       {
				if(bai1==0)
				{
                PC1  = 0;
				}
				else if(bai1==1)
				{
				PC1  = 1;
				}
        }	
	if(data2==0x07)
       {
				if(bai1==0)
				{
                PC3  = 0;
				}
				else if(bai1==1)
				{
				PC3  = 1;
				}
        }
	if(data2==0x08)
       {
				if(bai1==0)
				{
                PC5=0;
				}
				else if(bai1==1)
				{
				PC5=1;
				}
        }			
}

sbit   bit_status3;
sbit   bit_old3;
void Read_channes17_24state(UINT08 data3,UINT08 bai2)
{
       if(data3==1)
       {
				if(bai2==0)
				{
                PC7 = 0;
				}
				else if(bai2==1)
				{
				PC7 = 1;
				}
        }
		else if(data3==2)
       {
				if(bai2==0)
				{
                PB1 = 0;
				}
				else if(bai2==1)
				{
				PB1 = 1;
				}
        }
		else if(data3==3)
       {
				if(bai2==0)
				{
                PH0 = 0;
				}
				else if(bai2==1)
				{
				PH0 = 1;
				}
        }
		else if(data3==4)
       {
				if(bai2==0)
				{
                PH2 = 0;
				}
				else if(bai2==1)
				{
				PH2 = 1;
				}
        }
		else if(data3==5)
       {
				if(bai2==0)
				{
                PB3 = 0;
				}
				else if(bai2==1)
				{
				PB3 = 1;
				}
        }
		else if(data3==6)
       {
				if(bai2==0)
				{
                PB5 = 0;
				}
				else if(bai2==1)
				{
				PB5 = 1;
				}
        }
		else if(data3==7)
       {
				if(bai2==0)
				{
                PB7 = 0;
				}
				else if(bai2==1)
				{
				PB7 = 1;
				}
        }
		else if(data3==8)
       {
				if(bai2==0)
				{
                PA1 = 0;
				}
				else if(bai2==1)
				{
				PA1 = 1;
				}
        }
}
sbit   bit_status4;
sbit   bit_old4;
void Read_channes25_30state(UINT08 data4,UINT08 bai3)
{      
       if(data4==1)
       {
				if(bai3==0)
				{
                PA3 = 0;
				}
				else if(bai3==1)
				{
				PA3 = 1;
				}
				
        }
		else if(data4==2)
		{
				if(bai3==0)
				{
                PA5 = 0;
				}
				else if(bai3==1)
				{
				PA5 = 1;
				}
        }
		else if(data4==3)
		{
				if(bai3==0)
				{
                PA7 = 0;
				}
				else if(bai3==1)
				{
				PA7 = 1;
				}
		}
		else if(data4==4)
		{
				if(bai3==0)
				{
                PH5 = 0;
				}
				else if(bai3==1)
				{
				PH5 = 1;
				}
		}
		else if(data4==5)
		{
				if(bai3==0)
				{
					PH7 = 0;
				}
				else if(bai3==1)
				{
					PH7=1;
				}
		}
		else if(data4==6)
		{
				if(bai3==0)
				{
					PG1 = 0;
				}
				else if(bai3==1)
				{
					PG1 = 1;
				}
				
		}
}


/*
void Drive_sock(void)
{
	
      if(lock_number>=1 && lock_number<=8)
      {

      }
      else if(lock_number>=9 && lock_number<=16)
      {
        
      }
      else if(lock_number>=17 && lock_number<=24)
      {
         
      }
	  else if(lock_number>=25 && lock_number<=30)
      {
          
      }
	  
  lock_number=0;
}
*/
sbit bit_status5;

void Read_open_lock(void)
{

/*
              bit_status5=0;
              bit_status5 = PJ1;
              if(bit_status5 != 0)
              ClrBit(CH1_8,0);
              else
              SetBit(CH1_8,0);              

              bit_status5=0;
              bit_status5 = PF1;
              if(bit_status5 != 0)
              ClrBit(CH1_8,1);
              else
              SetBit(CH1_8,1);               
  
              bit_status5=0;
              bit_status5 = PF3;
              if(bit_status5 != 0)
              ClrBit(CH1_8,2);
              else
              SetBit(CH1_8,2);

			  bit_status5=0;
              bit_status5 = PJ2;
              if(bit_status5 != 0)
              ClrBit(CH1_8,3);
              else
              SetBit(CH1_8,3);

			  bit_status5=0;
              bit_status5 = PE4;   
              if(bit_status5 != 0)
              ClrBit(CH1_8,4);
              else
              SetBit(CH1_8,4);

			  bit_status5=0;
              bit_status5 = PG6;
              if(bit_status5 != 0)
              ClrBit(CH1_8,5);
              else
              SetBit(CH1_8,5);

			  bit_status5=0;
              bit_status5 = PD0;
              if(bit_status5 != 0)
              ClrBit(CH1_8,6);
              else
              SetBit(CH1_8,6);

			  bit_status5=0;
              bit_status5 = PD2;
              if(bit_status5 != 0)
              ClrBit(CH1_8,7);
              else
              SetBit(CH1_8,7);

			 bit_status5=0;
              bit_status5 = PD4;
              if(bit_status5 != 0)
              ClrBit(CH9_16,0);
              else
              SetBit(CH9_16,0);              

              bit_status5=0;
              bit_status5 = PD6;
              if(bit_status5 != 0)
              ClrBit(CH9_16,1);
              else
              SetBit(CH9_16,1);               
  
              bit_status5=0;
              bit_status5 = PJ4;
              if(bit_status5 != 0)
              ClrBit(CH9_16,2);
              else
              SetBit(CH9_16,2);

			  bit_status5=0;
              bit_status5 = PJ6;
              if(bit_status5 != 0)
              ClrBit(CH9_16,3);
              else
              SetBit(CH9_16,3);

			  bit_status5=0;
              bit_status5 = PC0;
              if(bit_status5 != 0)
              ClrBit(CH9_16,4);
              else
              SetBit(CH9_16,4);

			  bit_status5=0;
              bit_status5 = PC2;
              if(bit_status5 != 0)
              ClrBit(CH9_16,5);
              else
              SetBit(CH9_16,5);

			  bit_status5=0;
              bit_status5 = PC4;
              if(bit_status5 != 0)
              ClrBit(CH9_16,6);
              else
              SetBit(CH9_16,6);

			  bit_status5=0;
              bit_status5 = PC6;
              if(bit_status5 != 0)
              ClrBit(CH9_16,7);
              else
              SetBit(CH9_16,7);

			  bit_status5=0;
              bit_status5 = PB0;
              if(bit_status5 != 0)
              ClrBit(CH17_24,0);
              else
              SetBit(CH17_24,0);              

              bit_status5=0;
              bit_status5 = PB2;
              if(bit_status5 != 0)
              ClrBit(CH17_24,1);
              else
              SetBit(CH17_24,1);               
  
              bit_status5=0;
              bit_status5 = PH1;
              if(bit_status5 != 0)
              ClrBit(CH17_24,2);
              else
              SetBit(CH17_24,2);

			  bit_status5=0;
              bit_status5 = PH3;
              if(bit_status5 != 0)
              ClrBit(CH17_24,3);
              else
              SetBit(CH17_24,3);

			  bit_status5=0;
              bit_status5 = PB4;
              if(bit_status5 != 0)
              ClrBit(CH17_24,4);
              else
              SetBit(CH17_24,4);

			  bit_status5=0;
              bit_status5 = PB6;
              if(bit_status5 != 0)
              ClrBit(CH17_24,5);
              else
              SetBit(CH17_24,5);

			  bit_status5=0;
              bit_status5 = PA0;
              if(bit_status5 != 0)
              ClrBit(CH17_24,6);
              else
              SetBit(CH17_24,6);

			  bit_status5=0;
              bit_status5 = PA2;
              if(bit_status5 != 0)
              ClrBit(CH17_24,7);
              else
              SetBit(CH17_24,7);

			  bit_status5=0;
              bit_status5 = PA4;
              if(bit_status5 != 0)
              ClrBit(CH25_30,0);
              else
              SetBit(CH25_30,0);              

              bit_status5=0;
              bit_status5 = PA6;
              if(bit_status5 != 0)
              ClrBit(CH25_30,1);
              else
              SetBit(CH25_30,1);               
  
              bit_status5=0;
              bit_status5 = PH4;
              if(bit_status5 != 0)
              ClrBit(CH25_30,2);
              else
              SetBit(CH25_30,2);

			  bit_status5=0;
              bit_status5 = PH6;
              if(bit_status5 != 0)
              ClrBit(CH25_30,3);
              else
              SetBit(CH25_30,3);

			  bit_status5=0;
              bit_status5 = PG0;
              if(bit_status5 != 0)
              ClrBit(CH25_30,4);
              else
              SetBit(CH25_30,4);

			  bit_status5=0;
              bit_status5 = PG4;
              if(bit_status5 != 0)
              ClrBit(CH25_30,5);
              else
              SetBit(CH25_30,5);
			  */
}
